package com.company.OfferGuide;

import com.company.ListNode;

import java.util.Stack;

public class 反转链表 {

    public ListNode reverseList02(ListNode head) {
        if (head == null) return null;
        if (head.next == null) return head;

        ListNode p = head.next,pre = head,temp;
        pre.next = null;
        while (p != null) {
            temp = p.next;
            p.next = pre;
            pre = p;
            p = temp;
        }
        return pre;
    }

    public ListNode reverseList(ListNode head) {
        if (head == null) return null;
        if (head.next == null) return head;
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        ListNode newHead = stack.pop(),p = newHead;

        while (!stack.isEmpty()) {
            p.next = stack.pop();
            p = p.next;
            p.next = null;
        }
        return newHead;
    }

}
